import crawl from '../utils/crawl.mjs';
import db from '../mapper/db.mjs';
async function searchDataFromWeb() {
  const url = 'https://movie.douban.com/chart'

  const prompt = `
        # 职责
            获取前5条图片链接、电影名称、电影评分、电影简介
            输出格式为json 数组 
            如：下面三引号里面的json 数组
            """
            [
                {
                    src: "...",
                    title: "...",
                    score: "...",
                    desc: "..."
                },
                {
                    src: "...",
                    title: "...",
                    score: "...",
                    desc: "..."
                }
              
            ]    
            """
        # 注意
            输出json数组 , 严格遵循上述格式
`

  let crawlResult = await crawl.crawlDataFromWeb(url, prompt);
  console.log(crawlResult);


  if (crawlResult.length === 0) {
    return '爬取失败';
  }

  //转化为数组
  let res = Array.isArray(crawlResult) ? crawlResult : [crawlResult];

  if (res.length === 0) {
    return '数据库为空，没有爬取到数据...';
  }

  db.insertData(crawlResult);
  res = db.queryAllData();

  return res;
}

export default {
  searchDataFromWeb
}